local AttrDataHelper = {}
local FightConst = require("const.FightConst")
local AttributeConst = require("const.AttributeConst")

local ATTR_MODULE = {
    "HERO_LVUP",
    "EQUIP_LVUP"
}

function AttrDataHelper.getAllAttr(heroUnit)
    local list = {}
    for i = 1, #ATTR_MODULE do
        local moduleName = ATTR_MODULE[i]
        local attrFunc = AttrDataHelper["_" .. moduleName .. "_ATTR"]
        if attrFunc then
            local result = attrFunc(heroUnit)
            table.insert(list, result)
        end
    end
    local allAttr = {}
    for i = 1, #list do
        local resu = list[i]
        for id, value in pairs(resu) do
            if not allAttr[id] then
                allAttr[id] = 0
            end
            allAttr[id] = allAttr[id] + value
        end
    end
    return allAttr
end

function AttrDataHelper._HERO_LVUP_ATTR(heroUnit)
    local HeroDataHelper = require("data.helper.HeroDataHelper")
    local result = HeroDataHelper.getHeroUnitAttr(heroUnit)
    return result
end

function AttrDataHelper._EQUIP_LVUP_ATTR(heroUnit)
    local EquipDataHelper = require("data.helper.EquipDataHelper")
    local equipUnit = G_UserData:getBattle():getBattleEquipment(heroUnit:getPos())
    if equipUnit then
        local mainAttr, subAttr = EquipDataHelper.getEquipmentAttr(equipUnit)
        local result = {}
        result[mainAttr.id] = mainAttr.value
        result[subAttr.id] = subAttr.value
    end
    return result
end

function AttrDataHelper.getMonsterAttr(monsterId, level)
    local AttributeConst = require("const.AttributeConst")
    local cfg = require("config.monster").get(monsterId)
    local lvId = cfg.lvId
    local result = {}
    result[AttributeConst.BLOOD] = 0
    result[AttributeConst.ATTACK] = 0
    result[AttributeConst.DEFENSE] = 0
    for i = 1, level do
        local lvCfg = require("config.hero_lvup").get(lvId, i)
        result[AttributeConst.BLOOD] = result[AttributeConst.BLOOD] + lvCfg.hp_add
        result[AttributeConst.ATTACK] = result[AttributeConst.ATTACK] + lvCfg.atk_add
        result[AttributeConst.DEFENSE] = result[AttributeConst.DEFENSE] + lvCfg.def_add
    end
    return result
end

function AttrDataHelper.getRaceHurtAddAttrId(race)
    local RACE_ATTR = {
        [FightConst.RACE_TERRAN] = AttributeConst.HURT_ADD_REN,
        [FightConst.RACE_MOZU] = AttributeConst.HURT_ADD_MO,
        [FightConst.RACE_FAIRYCLAN] = AttributeConst.HURT_ADD_XIAN,
        [FightConst.RACE_YAOZU] = AttributeConst.HURT_ADD_YAO
    }
    return RACE_ATTR[race]
end

function AttrDataHelper.getSexHurtAttrId(sex)
    local SEX_ATTR = {
        [FightConst.SEX_MAN] = AttributeConst.HURT_ADD_NAN,
        [FightConst.SEX_WOMAN] = AttributeConst.HURT_ADD_NV
    }
    return SEX_ATTR[sex]
end

return AttrDataHelper
